package com.lxm.enigma.core;

import com.lxm.enigma.algorithm.AesAlgorithm;
import com.lxm.enigma.algorithm.Md5Algorithm;
import com.lxm.enigma.filter.EnigmaChainFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Lys
 * @Date 2022/5/24
 * @Describe
 **/
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties(EnigmaConfig.class)
public class EnigmaAutoConfiguration {

    @Bean
    public AesAlgorithm algorithm() {
        return new AesAlgorithm();
    }

    @Bean
    public Md5Algorithm md5Algorithm() {
        return new Md5Algorithm();
    }

    @Bean
    public FilterRegistrationBean<EnigmaChainFilter> filterRegistration(EnigmaConfig enigmaConfig, AesAlgorithm aesAlgorithm) {
        var registration = new FilterRegistrationBean<EnigmaChainFilter>();
        registration.setFilter(new EnigmaChainFilter(enigmaConfig, aesAlgorithm));
        registration.addUrlPatterns(enigmaConfig.getUrlPatterns());
        registration.setName("enigma-filter");
        return registration;
    }

    @Bean
    public CipherMonitor cipherMonitor(EnigmaConfig enigmaConfig) {
        return new CipherMonitor(enigmaConfig);
    }

    @Bean
    public CipherRegister cipherRegister() {
        return new CipherRegister();
    }
}
